﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.Platform.Mvc.Models;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    public class OrgController : Controller
    {
        #region Index
        [AutomaticTemplate]
        public ActionResult Index()
        {
            var newOrgList = new List<OrgIndexViewModel>();
            IEnumerable<OrgDTO> list;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                list = proxy.Channel.GetOrgs();
            }

            Build(list, newOrgList, null);
            //return View(new FiledIndexViewModel() { Data = newList });
            return View(new OrgIndexViewModel() { Data = newOrgList });
        }

        private void Build(IEnumerable<OrgDTO> table, List<OrgIndexViewModel> node, Guid? parentValue)
        {
            var rows = table.Where(x => x.ParentId == parentValue).OrderBy(x => x.OrderIndex);
            foreach (var row in rows)
            {
                var _node = new OrgIndexViewModel();
                _node.Title = row.Title;
                _node.Id = row.Id;
                _node.ParentId = row.ParentId;
                if (row.ParentId == null)
                {
                    _node.Itemclass = string.Format("treegrid-{0}", row.Id);
                }
                else
                {
                    _node.Itemclass = string.Format("treegrid-{0} treegrid-parent-{1}", row.Id, row.ParentId);
                }


                node.Add(_node);

                Build(table, node, row.Id);
            }
        }

        #endregion

        private void Build(IEnumerable<OrgDTO> table, zTreeNode node, Guid? parentValue)
        {
            var rows = table.Where(x => x.ParentId == parentValue).OrderBy(x => x.OrderIndex);
            foreach (var row in rows)
            {
                var _node = new zTreeNode
                {
                    Name = row.Title,
                    Id = row.Id.ToString(),
                    ParentId = row.ParentId == null ? row.ParentId.ToString() : null,
                    Raw = row
                };

                node.Children.Add(_node);
                node.IsParent = node.Children.Any();

                Build(table, _node, row.Id);
            }
        }

        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var model = new OrgViewModel
                {
                    OrgLevelList = proxy.Channel.GetOrgLevelList(),
                    TreeNodes = new zTreeNode()
                };
                Build(proxy.Channel.GetOrgs(), model.TreeNodes, null);

                return View("Edit", model);
            }
        }

        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var model = proxy.Channel.GetOrgInfo(id).To<OrgViewModel>();
                model.OrgLevelList = proxy.Channel.GetOrgLevelList();
                model.TreeNodes = new zTreeNode();
                if (model.ParentId.HasValue)
                {
                    model.ParentText = proxy.Channel.GetOrgInfo(model.ParentId.Value).Title;
                }
                Build(proxy.Channel.GetOrgs(), model.TreeNodes, null);
                return View("Edit", model);
            }
        }

        [HttpPost]
        public ActionResult Save(OrgDTO vm)
        {
            if (vm == null)
            {
                Json(new Result() { Err = "提交数据出错。" });
            }
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.SaveOrg(vm);
                res.RedirectToUrl = Url.Action("Index");

                return this.JsonNet(res);
            }
        }

        [Permission(Name = "删除", Order = 3)]
        [HttpPost]
        public ActionResult Delete(Guid[] ids)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.DeleteOrgs(ids);
                res.RedirectToUrl = Url.Action("Index");

                return this.JsonNet(res);
            }
        }

    }
}